package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.TabTaskMapper;
import com.powergisol.gis.entity.TabTask;
import com.powergisol.gis.service.TabTaskService;
import com.powergisol.gis.util.DateUtils;
import com.powergisol.gis.vo.TabTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created By marc
 * Date: 2020/9/25  11:18
 * Description:
 */
@Service
public class TabTaskServiceImpl implements TabTaskService {

    public static final Double NUM = 100d;
    public static final Double NUM_0 = 0d;

    @Autowired
    private TabTaskMapper tabTaskMapper;

    @Override
    public List<TabTask> getByState(Integer taskState, Integer taskType) {
        TabTaskVo vo = new TabTaskVo();
        vo.setTaskState(taskState);
        vo.setTaskType(taskType);
        return tabTaskMapper.getByState(vo);
    }

    @Override
    public List<TabTask> getTaskList(TabTaskVo vo) {
        String timeStr = DateUtils.dateToString(new Date(),"yyyy-MM-dd");
        vo.setStartTime(timeStr);
        List<TabTask> taskList = tabTaskMapper.getByState(vo);
        for (TabTask task:taskList) {
            if(TabTask.TASK_STATE_1.equals(task.getTaskState()) && task.getProgress()<NUM){
                task.setProgress(NUM);
                tabTaskMapper.upTabTask(task);
            }
            if(TabTask.TASK_STATE_2.equals(task.getTaskState())){
                task.setProgress(NUM_0);
                tabTaskMapper.upTabTask(task);
            }
        }
        return taskList;
    }

    @Override
    public void upTask(TabTask tabTask) {
        tabTaskMapper.upTabTask(tabTask);
    }
}
